package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.PasswdUtils;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.common.controller.ApittController;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComVO;
import com.jinzhi.jzweb.pojo.OneLevelComVO;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserRoleService;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * <pre>
 * 考试答卷
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetCom")
public class ExaminationAnswerSheetComController extends BaseController {
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetComService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private ExaminationAnswerSheetComService examinationAnswerSheetService;
    @Autowired
    private ExaminationPaperService examinationPaperService;
    @Autowired
    private StuSignUpComService stuSignUpService;
    @Autowired
    private ExaminationQuestionsService examinationQuestionsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationResultsComService resultsService;
    @Autowired
    private ExaminationResultsComService examinationResultsService;
    @Autowired
    private ApittController apittController;
    @Autowired
    private UserService userService;





    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetCom")
    String info( Model model) {
        Wrapper<ExaminationAnswerSheetComDO> exWrapper = new EntityWrapper<ExaminationAnswerSheetComDO>();
        List<ExaminationAnswerSheetComVO> sheetComVOList =new ArrayList<>();
        exWrapper.orderBy("id",false);
        List<ExaminationAnswerSheetComDO>  examinationPaperDOS = examinationAnswerSheetService.selectList(exWrapper);
        for(ExaminationAnswerSheetComDO li:examinationPaperDOS){
            ExaminationAnswerSheetComVO answerSheetComVO=new ExaminationAnswerSheetComVO();
            List<Map<String,Object>> sheets =new ArrayList<>();
            ExaminationResultsComDO examinationPaperDO = examinationResultsService.findOneByKv("stu_sign_id", li.getStuSignId());
            String vauel=StringEscapeUtils.unescapeJava(li.getAnswerList());
            vauel.replaceAll("\r","").replaceAll("\t","").replaceAll("\n","");
            try {

                JSONArray jsonArray =  JSONObject.parseArray(vauel);

                StuSignUpComDO stuSignUpComDO=  stuSignUpService.selectById(li.getStuSignId());
                if(stuSignUpComDO!=null){
                    List<ExaminationAnswerSheetComVO> sheeets  = null;
                    for(int i=0;i<jsonArray.size();i++){

                        JSONObject jsonObj = jsonArray.getJSONObject(i);
                        String type = jsonObj.getString("type");
                        if(type.equals("4")){
                            if(stuSignUpComDO!=null){
                                String id = jsonObj.getString("id");
                                ExaminationQuestionsDO examinationQuestionsDO=   examinationQuestionsService.selectById(id);
                                if(examinationQuestionsDO!=null){
                                    answerSheetComVO.setContent(examinationQuestionsDO.getIntroduce());
                                }else {
                                    answerSheetComVO.setContent("");
                                }

                                answerSheetComVO.setAnswerList(jsonObj.getString("value"));
                                answerSheetComVO.setState(jsonObj.getString("state"));
                                answerSheetComVO.setNum2(stuSignUpComDO.getNum2());
                                answerSheetComVO.setCourt(stuSignUpComDO.getCourt());
                                answerSheetComVO.setId(stuSignUpComDO.getId());
                                answerSheetComVO.setExamBatchId(stuSignUpComDO.getCbatchId());
                                if(examinationPaperDO==null){
                                    answerSheetComVO.setFractionA(false);
                                    answerSheetComVO.setFractionB(false);
                                    answerSheetComVO.setFractionC(false);
                                    answerSheetComVO.setFractionD(false);
                                    answerSheetComVO.setFractionE(false);
                                }else {
                                    if(examinationPaperDO.getScore1()==null){
                                        answerSheetComVO.setFractionA(false);
                                    }else {
                                        if(examinationPaperDO.getScore1()<1){
                                            answerSheetComVO.setFractionA(false);
                                        }else {
                                            answerSheetComVO.setFractionA(true);
                                        }
                                    }

                                    if(examinationPaperDO.getScore2()==null){
                                        answerSheetComVO.setFractionB(false);
                                    }else {
                                        if(examinationPaperDO.getScore2()<1){
                                            answerSheetComVO.setFractionB(false);
                                        }else {
                                            answerSheetComVO.setFractionB(true);
                                        }
                                    }
                                    if(examinationPaperDO.getScore3()==null){
                                        answerSheetComVO.setFractionC(false);
                                    }else {
                                        if(examinationPaperDO.getScore3()<1){
                                            answerSheetComVO.setFractionC(false);
                                        }else {
                                            answerSheetComVO.setFractionC(true);
                                        }
                                    }
                                    answerSheetComVO.setScore1(examinationPaperDO.getScore1());
                                    answerSheetComVO.setScore2(examinationPaperDO.getScore2());
                                    answerSheetComVO.setScore3(examinationPaperDO.getScore3());
                                    answerSheetComVO.setScore4(examinationPaperDO.getScore4());
                                    answerSheetComVO.setScore5(examinationPaperDO.getScore5());

                                    answerSheetComVO.setScore1s(examinationPaperDO.getScore1s());
                                    answerSheetComVO.setScore2s(examinationPaperDO.getScore2s());
                                    answerSheetComVO.setScore3s(examinationPaperDO.getScore3s());
                                    answerSheetComVO.setScore4s(examinationPaperDO.getScore4s());
                                    answerSheetComVO.setScore5s(examinationPaperDO.getScore5s());
                                }
                            }
                        }
                        Map map = new HashMap();
                        map.put("content",answerSheetComVO.getContent());
                        map.put("answerList",answerSheetComVO.getAnswerList());
                        sheets.add(map);
                        answerSheetComVO.setSheeets(sheets);
                    }
                    sheetComVOList.add(answerSheetComVO);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        sheetComVOList = sheetComVOList.stream().sorted(Comparator.comparing(ExaminationAnswerSheetComVO::getNum2)).collect(Collectors.toList());
        model.addAttribute("examinationAnswerSheet", sheetComVOList);
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetCom";
    }


    /**
     * 一级管理员登录验证
     * @return
     */
    @Log("一级管理员现场改分")
    @PostMapping( "/oneLevelLogin")
    @ResponseBody
    public Result<String>  oneLevelLogin(String userName,String passWord){
        Wrapper<UserDO> uWrapper = new EntityWrapper<UserDO>().eq("username",userName);
        List<UserDO>  userDOS = userService.selectList(uWrapper);
        if(userDOS==null){
            return Result.fail("用户不存在");
        }
        if(userDOS.size()>1 || userDOS.isEmpty()){
            return Result.fail("用户名密码输入错误");
        }
        List<String> organizationNewsDOS = userRoleService.selroleNameList(userDOS.get(0).getId());
        for(String name:organizationNewsDOS){
            if(!name.contains("管理员")){
                return Result.fail("暂无权限！");
            }
        }


        String salt = userDOS.get(0).getSalt();
    //    String encodePasswd = PasswdUtils.get(userDOS.get(0).getPassword(), salt);
        String encodePasswd1 = PasswdUtils.get(passWord, salt);
        if(encodePasswd1.equals(userDOS.get(0).getPassword())){
            return Result.ok();
        }else {
            return Result.fail("暂无权限！");
        }


    }




    /**
     * 一级管理员现场改分
     * @return
     */
    @Log("一级管理员现场改分")
    @GetMapping( "/oneLevelUpdateCom")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:oneLevelUpdateCom")
    public Result<String>  oneLevelUpdateCom(OneLevelComVO oneLevelComVO){
        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", oneLevelComVO.getId());
        //获取考试批次
     //   ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",oneLevelComVO.getExamBatchId());
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", oneLevelComVO.getId());
        if(examinationPaperDO!=null){
            ExaminationResultsComDO examinationResultsDO = examinationPaperDO;

            if(oneLevelComVO.getFractionC()>0){
                examinationResultsDO.setScore3(oneLevelComVO.getFractionC());
            }
            if(oneLevelComVO.getFractionB()>0){
                examinationResultsDO.setScore2(oneLevelComVO.getFractionB()); }
            if(oneLevelComVO.getFractionA()>0){
                examinationResultsDO.setScore1(oneLevelComVO.getFractionA());
            }
            boolean b = examinationResultsService.updateById(examinationResultsDO);
            return b==true?Result.ok():Result.fail();
        }


        return Result.fail("修改失败，未进行阅卷！");
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetCom")
    public Result<Page<ExaminationAnswerSheetComDO>> list(ExaminationAnswerSheetComDO examinationAnswerSheetComDTO){
        // 查询列表数据
        Page<ExaminationAnswerSheetComDO> page = examinationAnswerSheetComService.selectPage(getPage(ExaminationAnswerSheetComDO.class),
                examinationAnswerSheetComService.convertToEntityWrapper().orderBy("id", false));
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExaminationAnswerSheetComDO obj = (ExaminationAnswerSheetComDO) page.getRecords().get(i);
            list.add(obj);
        }
        page.setRecords(list);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:add")
    String add(){
        return "jzweb/examinationAnswerSheetCom/add";
    }


    /**
     * 裁判A 打分
     * @return
     */
    @GetMapping("/fractionA")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:fractionA")
    Result<String>  fractionA(Long id,Double fractionA1,Double fractionA2,Long examBatchId){

        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", id);
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examBatchId);
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", id);
        if(examinationPaperDO==null){

            ExaminationResultsComDO examinationResultsDO = new ExaminationResultsComDO();
            examinationResultsDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
            examinationResultsDO.setEbatchId(examBatchId);//考试批次id
            examinationResultsDO.setStuSignId(id);//考生信息id
            if(examinationAnswerSheetDOs == null){
                examinationResultsDO.setState(1);//缺考
            }else{

                examinationResultsDO.setState(0);//正常
            }
            double result = 0;
            ExaminationAnswerSheetComDO examinationAnswerSheetDO = null;
            if(examinationAnswerSheetDOs!=null && examinationAnswerSheetDOs.size()>0) {
                examinationAnswerSheetDO = examinationAnswerSheetDOs.get(0);
                if(examinationPaperDO!=null){
                    List<Long> idss = examinationQuestionsService.selIdList(examinationPaperDO.getId());
                    //试卷对应的试题，有试题才算分，没有直接0分处理；
                    if (idss != null && idss.size() > 0) {
                        if (examinationAnswerSheetDO != null) {
                            //有答卷才算分，没有直接返回0
                            result = results(examinationAnswerSheetDO.getAnswerList(), idss);//考试总分

                        }
                    }
                }

            }
            examinationResultsDO.setScore1(fractionA1);
            examinationResultsDO.setScore1s(fractionA2);
            examinationResultsDO.setTotalScore(result);//考试成绩
            boolean b = examinationResultsService.insert(examinationResultsDO);

            return b==true?Result.ok():Result.fail();
        }
        if(examinationPaperDO.getScore1()!=null){
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            for(String name:organizationNewsDOS){
                if(name.contains("裁判")){
                    return Result.fail("您已经完成该考生已经完成评分！");
                }
            }
        }
        examinationPaperDO.setScore1(fractionA1);
        examinationPaperDO.setScore1s(fractionA2);
      boolean b = resultsService.updateById(examinationPaperDO);
        return b==true?Result.ok():Result.fail();
    }

    /**
     * 裁判B 打分
     * @return
     */
    @GetMapping("/fractionB")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:fractionB")
    Result<String>  fractionB(Long id,Double fractionB1,Double fractionB2,Long examBatchId){

        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", id);
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examBatchId);
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", id);
        if(examinationPaperDO==null){

            ExaminationResultsComDO examinationResultsDO = new ExaminationResultsComDO();
            examinationResultsDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
            examinationResultsDO.setEbatchId(examBatchId);//考试批次id
            examinationResultsDO.setStuSignId(id);//考生信息id
            if(examinationAnswerSheetDOs == null){
                examinationResultsDO.setState(1);//缺考
            }else{

                examinationResultsDO.setState(0);//正常
            }
            double result = 0;
            ExaminationAnswerSheetComDO examinationAnswerSheetDO = null;
            if(examinationAnswerSheetDOs!=null && examinationAnswerSheetDOs.size()>0) {
                examinationAnswerSheetDO = examinationAnswerSheetDOs.get(0);
                if(examinationPaperDO!=null){
                    List<Long> idss = examinationQuestionsService.selIdList(examinationPaperDO.getId());
                    //试卷对应的试题，有试题才算分，没有直接0分处理；
                    if (idss != null && idss.size() > 0) {
                        if (examinationAnswerSheetDO != null) {
                            //有答卷才算分，没有直接返回0
                            result = results(examinationAnswerSheetDO.getAnswerList(), idss);//考试总分
                        }
                    }
                }

            }
            examinationResultsDO.setScore2(fractionB1);
            examinationResultsDO.setScore2s(fractionB2);
            examinationResultsDO.setTotalScore(result);//考试成绩
            boolean b = examinationResultsService.insert(examinationResultsDO);

            return b==true?Result.ok():Result.fail();
        }
        if(examinationPaperDO.getScore2()!=null){
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            for(String name:organizationNewsDOS){
                if(name.contains("裁判")){
                    return Result.fail("您已经完成该考生已经完成评分！");
                }
            }
        }
        examinationPaperDO.setScore2(fractionB1);
        examinationPaperDO.setScore2s(fractionB2);
        boolean b = resultsService.updateById(examinationPaperDO);
        return b==true?Result.ok():Result.fail();
    }


    /**
     * 裁判C 打分
     * @return
     */
    @GetMapping("/fractionC")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:fractionC")
    Result<String>  fractionC(Long id,Double fractionC1,Double fractionC2,Long examBatchId){

        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", id);
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examBatchId);
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", id);
        if(examinationPaperDO==null){

            ExaminationResultsComDO examinationResultsDO = new ExaminationResultsComDO();
            examinationResultsDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
            examinationResultsDO.setEbatchId(examBatchId);//考试批次id
            examinationResultsDO.setStuSignId(id);//考生信息id
            if(examinationAnswerSheetDOs == null){
                examinationResultsDO.setState(1);//缺考
            }else{

                examinationResultsDO.setState(0);//正常
            }
            double result = 0;
            ExaminationAnswerSheetComDO examinationAnswerSheetDO = null;
            if(examinationAnswerSheetDOs!=null && examinationAnswerSheetDOs.size()>0) {
                examinationAnswerSheetDO = examinationAnswerSheetDOs.get(0);
                if(examinationPaperDO!=null){
                    List<Long> idss = examinationQuestionsService.selIdList(examinationPaperDO.getId());
                    //试卷对应的试题，有试题才算分，没有直接0分处理；
                    if (idss != null && idss.size() > 0) {
                        if (examinationAnswerSheetDO != null) {
                            //有答卷才算分，没有直接返回0
                            result = results(examinationAnswerSheetDO.getAnswerList(), idss);//考试总分

                        }
                    }
                }

            }
            examinationResultsDO.setScore3(fractionC1);
            examinationResultsDO.setScore3s(fractionC2);
            examinationResultsDO.setTotalScore(result);//考试成绩
            boolean b = examinationResultsService.insert(examinationResultsDO);
            return b==true?Result.ok():Result.fail();
        }
        if(examinationPaperDO.getScore3()!=null){
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            for(String name:organizationNewsDOS){
                if(name.contains("裁判")){
                    return Result.fail("您已经完成该考生已经完成评分！");
                }
            }
        }
        examinationPaperDO.setScore3(fractionC1);
        examinationPaperDO.setScore3s(fractionC2);
        boolean b = resultsService.updateById(examinationPaperDO);
        return b==true?Result.ok():Result.fail();
    }

    /**
     * 裁判D 打分
     * @return
     */
    @GetMapping("/fractionD")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:fractionD")
    Result<String>  fractionD(Long id,Double fractionD1,Double fractionD2,Long examBatchId){

        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", id);
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examBatchId);
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", id);
        if(examinationPaperDO==null){

            ExaminationResultsComDO examinationResultsDO = new ExaminationResultsComDO();
            examinationResultsDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
            examinationResultsDO.setEbatchId(examBatchId);//考试批次id
            examinationResultsDO.setStuSignId(id);//考生信息id
            if(examinationAnswerSheetDOs == null){
                examinationResultsDO.setState(1);//缺考
            }else{

                examinationResultsDO.setState(0);//正常
            }
            double result = 0;
            ExaminationAnswerSheetComDO examinationAnswerSheetDO = null;
            if(examinationAnswerSheetDOs!=null && examinationAnswerSheetDOs.size()>0) {
                examinationAnswerSheetDO = examinationAnswerSheetDOs.get(0);
                if(examinationPaperDO!=null){
                    List<Long> idss = examinationQuestionsService.selIdList(examinationPaperDO.getId());
                    //试卷对应的试题，有试题才算分，没有直接0分处理；
                    if (idss != null && idss.size() > 0) {
                        if (examinationAnswerSheetDO != null) {
                            //有答卷才算分，没有直接返回0
                            result = results(examinationAnswerSheetDO.getAnswerList(), idss);//考试总分

                        }
                    }
                }

            }
            examinationResultsDO.setScore4(fractionD1);
            examinationResultsDO.setScore4s(fractionD2);
            examinationResultsDO.setTotalScore(result);//考试成绩
            boolean b = examinationResultsService.insert(examinationResultsDO);

            return b==true?Result.ok():Result.fail();
        }
        if(examinationPaperDO.getScore1()!=null){
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            for(String name:organizationNewsDOS){
                if(name.contains("裁判")){
                    return Result.fail("您已经完成该考生已经完成评分！");
                }
            }
        }
        examinationPaperDO.setScore4(fractionD1);
        examinationPaperDO.setScore4s(fractionD2);
        boolean b = resultsService.updateById(examinationPaperDO);
        return b==true?Result.ok():Result.fail();
    }


    /**
     * 裁判E 打分
     * @return
     */
    @GetMapping("/fractionE")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:fractionE")
    Result<String>  fractionE(Long id,Double fractionE1,Double fractionE2,Long examBatchId){

        List<ExaminationAnswerSheetComDO> examinationAnswerSheetDOs = examinationAnswerSheetService
                .findByKv("stu_sign_id", id);
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examBatchId);
        ExaminationResultsComDO examinationPaperDO = resultsService.findOneByKv("stu_sign_id", id);
        if(examinationPaperDO==null){

            ExaminationResultsComDO examinationResultsDO = new ExaminationResultsComDO();
            examinationResultsDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
            examinationResultsDO.setEbatchId(examBatchId);//考试批次id
            examinationResultsDO.setStuSignId(id);//考生信息id
            if(examinationAnswerSheetDOs == null){
                examinationResultsDO.setState(1);//缺考
            }else{

                examinationResultsDO.setState(0);//正常
            }
            double result = 0;
            ExaminationAnswerSheetComDO examinationAnswerSheetDO = null;
            if(examinationAnswerSheetDOs!=null && examinationAnswerSheetDOs.size()>0) {
                examinationAnswerSheetDO = examinationAnswerSheetDOs.get(0);
                if(examinationPaperDO!=null){
                    List<Long> idss = examinationQuestionsService.selIdList(examinationPaperDO.getId());
                    //试卷对应的试题，有试题才算分，没有直接0分处理；
                    if (idss != null && idss.size() > 0) {
                        if (examinationAnswerSheetDO != null) {
                            //有答卷才算分，没有直接返回0
                            result = results(examinationAnswerSheetDO.getAnswerList(), idss);//考试总分

                        }
                    }
                }

            }
            examinationResultsDO.setScore5(fractionE1);
            examinationResultsDO.setScore5s(fractionE2);
            examinationResultsDO.setTotalScore(result);//考试成绩
            boolean b = examinationResultsService.insert(examinationResultsDO);

            return b==true?Result.ok():Result.fail();
        }
        if(examinationPaperDO.getScore1()!=null){
            List<String> organizationNewsDOS = userRoleService.selroleNameList(this.getUserId());
            for(String name:organizationNewsDOS){
                if(name.contains("裁判")){
                    return Result.fail("您已经完成该考生已经完成评分！");
                }
            }
        }
        examinationPaperDO.setScore5(fractionE1);
        examinationPaperDO.setScore5s(fractionE2);
        boolean b = resultsService.updateById(examinationPaperDO);
        return b==true?Result.ok():Result.fail();
    }


    private double results(String answers,List<Long> idss) {
        double results = 0;
        if(StringUtils.isNotBlank(answers)){
            net.sf.json.JSONArray jsonArray2 = net.sf.json.JSONArray.fromObject(answers);
            for(int i = 0;i<jsonArray2.size();i++){
                if(idss.indexOf(Long.parseLong(jsonArray2.getJSONObject(i).getString("id"))) > -1){
                    ExaminationQuestionsDO examinationQuestionsDO = examinationQuestionsService.selectById(Long.parseLong(jsonArray2.getJSONObject(i).getString("id")));
                    if(!jsonArray2.getJSONObject(i).containsKey("value"))continue;
                    if (examinationQuestionsDO.getType() == 0 || examinationQuestionsDO.getType() == 2) {//单选题和判断题，答案唯一，可以不用拆解了
                        if (examinationQuestionsDO.getAnswers().equals(jsonArray2.getJSONObject(i).getString("value"))) {
                            results = results + examinationQuestionsDO.getScore();
                        }
                    } else if (examinationQuestionsDO.getType() == 1) {//多选题因为答案有多个，需要循环判断一下是否包含该答案
                        String[] ssb = jsonArray2.getJSONObject(i).getString("value").split(";;");
                        String answer = "";
                        for (int n = 0; n < ssb.length; n++) {
                            if (answer == "") {
                                answer = ssb[n];
                            } else {
                                answer = answer + ";;" + ssb[n];
                            }
                        }

                        if (examinationQuestionsDO.getAnswers().equals(answer)) {
                            results = results + examinationQuestionsDO.getScore();
                        }

                    }
                }
            }
        }

        return results;
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:edit")
    String edit(@PathVariable("id") Long id,Model model) {


        ExaminationAnswerSheetComDO examinationAnswerSheetCom = examinationAnswerSheetComService.selectById(id);
        ExaminationAnswerSheetComVO answerSheetComVO = new ExaminationAnswerSheetComVO();
        String vauel = examinationAnswerSheetCom.getAnswerList();
        JSONArray jsonArray = JSONObject.parseArray(vauel);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            String type = jsonObj.getString("type");
            if (type.equals("4")) {
                StuSignUpComDO stuSignUpComDO = stuSignUpService.selectById(examinationAnswerSheetCom.getStuSignId());
                if (stuSignUpComDO != null) {
                    answerSheetComVO.setAnswerList(jsonObj.getString("value"));
                    answerSheetComVO.setState(stuSignUpComDO.getState().toString());
                    answerSheetComVO.setNum2(stuSignUpComDO.getNum2());
                    answerSheetComVO.setCourt(stuSignUpComDO.getCourt());
                    answerSheetComVO.setId(stuSignUpComDO.getId());
                    answerSheetComVO.setExamBatchId(stuSignUpComDO.getCbatchId());
                }
                model.addAttribute("answerSheetComVO", answerSheetComVO);
            }
        }
        return "jzweb/examinationAnswerSheetCom/edit";
    }
    @Log("添加考试答卷")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:add")
    public Result<String> save( ExaminationAnswerSheetComDO examinationAnswerSheetCom){
        examinationAnswerSheetComService.insert(examinationAnswerSheetCom);
        return Result.ok();
    }

    @Log("修改考试答卷")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:edit")
    public Result<String>  update( ExaminationAnswerSheetComDO examinationAnswerSheetCom){
        boolean update = examinationAnswerSheetComService.updateById(examinationAnswerSheetCom);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考试答卷")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:remove")
    public Result<String>  remove( Long id){
        examinationAnswerSheetComService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考试答卷")
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:batchRemove")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        examinationAnswerSheetComService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }





    /**
     * 工作人员打分
     * @return
     */
    @PostMapping("/updateScore")
    @ResponseBody
    Result<String> updateScore(@RequestBody ExaminationResultsComDO examinationResultsComDO){
        //获取考试批次
        ExaminationBatchDO examinationBatchDO = examinationBatchService.findOneByKv("id",examinationResultsComDO.getCbatchId());
        //如果当前时间小于结束时间无法打分
        if(new Date().compareTo(examinationBatchDO.getEndTime()) <= 0){
            Result.fail("竞赛未结束，无法录入分数");
        }
        examinationResultsComDO.setCbatchId(examinationBatchDO.getCbatchId());//认证批次id
        examinationResultsComDO.setEbatchId(examinationBatchDO.getId());//考试批次id
        examinationResultsComDO.setState(0);//正常
        return resultsService.insertOrUpdate(examinationResultsComDO)==true?Result.ok():Result.fail();
    }


    /**
     * 分数回显
     * @return
     */
    @GetMapping("/getScoreById")
    @ResponseBody
    Result<Object> getScoreById(Long stuSignId){
        ExaminationResultsComDO resultsComDO = new ExaminationResultsComDO();
        resultsComDO.setStuSignId(stuSignId);
        //获取考试批次
        return Result.ok(resultsService.selectOne(resultsComDO));
    }





}
